package com.LeeCode;

import java.util.*;

/**
 * 成为 K 特殊字符串需要删除的最少字符数
 */

public class Code3085 {
    public static void main(String[] args) {
        String word = "aabcaba";
        int k = 0;
        System.out.println(new Code3085().minimumDeletions(word, k));
    }

    public int minimumDeletions(String word, int k) {
        char[] chars = word.toCharArray();
        int[] cnt = new int[26];
        for (char c : chars) {
            cnt[c - 'a']++;
        }
        Arrays.sort(cnt);
        int maxSave = 0;
        for (int i = 0; i < cnt.length; i++) {
            int sum = 0;
            for (int j = i; j < cnt.length; j++) {
                sum += Math.min(cnt[j], cnt[i] + k);
            }
            maxSave = Math.max(maxSave, sum);
        }

        return word.length() - maxSave;
    }
}
